package com.schedule.schedule.controller;


import com.schedule.classroom.entity.ClassRoom;
import com.schedule.classroom.service.ClassRoomService;
import com.schedule.common.CommonResult;
import com.schedule.course.entity.Course;
import com.schedule.course.service.CourseService;
import com.schedule.schedule.entity.ListParm;
import com.schedule.schedule.entity.ScheduleCourse;
import com.schedule.schedule.entity.ScheduleParm;
import com.schedule.schedule.entity.SelectOption;
import com.schedule.schedule.service.ScheduleCourseService;
import com.schedule.teacher.entity.Teacher;
import com.schedule.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/schedule")
public class ScheduleCourseController {
    @Autowired
    private ScheduleCourseService scheduleCourseService;
    @Autowired
    private ClassRoomService classRoomService;
    @Autowired
    private CourseService courseService;
    @Autowired
    private TeacherService teacherService;


    @GetMapping("/getRoomList")
    public CommonResult<List<SelectOption>> getRoomList(){
        //查询list
        List<ClassRoom> list = classRoomService.list();
        //组装前端需要的数据格式
        List<SelectOption> selectOptions = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .forEach(item->{
                    SelectOption option = new SelectOption();
                    option.setValue(item.getRoomId());
                    option.setLabel(item.getRoomName());
                    selectOptions.add(option);
        });
        return CommonResult.success("查询成功",selectOptions);
    }

    @GetMapping("/getTeacher")
    public CommonResult<List<SelectOption>> getTeacher(){
        List<Teacher> list = teacherService.list();
        List<SelectOption> selectOptions = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .forEach(item->{
                    SelectOption option = new SelectOption();
                    option.setValue(item.getTeacherId());
                    option.setLabel(item.getTeacherName());
                    selectOptions.add(option);
        });
        return CommonResult.success("查询成功",selectOptions);
    }

    @GetMapping("/getCourseList")
    public CommonResult<List<SelectOption>> getCourseList(){
        List<Course> list = courseService.list();
        List<SelectOption> selectOptions = new ArrayList<>();
        Optional.ofNullable(list).orElse(new ArrayList<>())
                .forEach(item->{
                    SelectOption option = new SelectOption();
                    option.setValue(item.getCourseId());
                    option.setLabel(item.getCourseName());
                    selectOptions.add(option);
        });
        return CommonResult.success("查询成功",selectOptions);
    }

    /**
     * 保存排课
     * @author: 清梦
     * @param scheduleParm
     * @return {@link CommonResult }
     * @dateTime: 2022/4/6 15:32
     */
    @PostMapping("/saveSchedule")
    public CommonResult<String> saveSchedule(@RequestBody ScheduleParm scheduleParm){
        scheduleCourseService.saveSchedule(scheduleParm);
        return CommonResult.success("排课成功!");
    }

    /**
     * 排课列表查询
     * @author: 清梦
     * @param listParam
     * @return {@link CommonResult< List< ScheduleCourse>> }
     * @dateTime: 2022/4/6 15:34
     */
    @GetMapping("/getScheduleList")
    public CommonResult<List<ScheduleCourse>> getScheduleList(ListParm listParam){
        List<ScheduleCourse> list = scheduleCourseService.selectCourseSchedulingList(listParam);
        return CommonResult.success("查询成功",list);
    }


    @PostMapping("/addSchedule")
    public CommonResult<String> addSchedule(@RequestBody ScheduleCourse scheduleCourse){
        boolean save = scheduleCourseService.save(scheduleCourse);
        if(save){
            return CommonResult.success("排课成功!");
        }
        return CommonResult.error("排课失败!");
    }



    @PutMapping("/updateScheduleById")
    public CommonResult<String> updateScheduleById(@RequestBody ScheduleCourse scheduleCourse){
        boolean save = scheduleCourseService.updateById(scheduleCourse);
        if(save){
            return CommonResult.success("编辑成功!");
        }
        return CommonResult.error("编辑失败!");
    }
}
